package tagcloud.gui;

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.Insets;
import tagcloud.core.*;

public class WordElementPane extends JPanel implements ChangeListener {

	private static final long serialVersionUID = 1L;
	private JCheckBox selectedCheckBox = null;
	private JLabel textLabel = null;
	private JLabel countLabel = null;
	private Word word;
	
	private static final Color DEFAULTCOLOR = new Color(238, 238, 238);  //  @jve:decl-index=0:
	private static final Color SELECTEDCOLOR = new Color(193, 232, 238);  //  @jve:decl-index=0:

	/**
	 * This is the default constructor
	 */
	public WordElementPane(Word word, boolean selected) {
		super();
		initialize();
		this.word = word;
		this.textLabel.setText(word.getText());
		this.countLabel.setText(word.getNumOccurences() + "");
		
		this.setBackground((selected ? SELECTEDCOLOR : DEFAULTCOLOR));
		this.selectedCheckBox.setSelected(selected);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 2;
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.weightx = 1.0D;
		gridBagConstraints2.insets = new Insets(0, 4, 0, 4);
		gridBagConstraints2.gridy = 0;
		countLabel = new JLabel();
		countLabel.setText("##");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 1;
		gridBagConstraints1.weightx = 1.0D;
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.insets = new Insets(0, 4, 0, 4);
		gridBagConstraints1.gridy = 0;
		textLabel = new JLabel();
		textLabel.setText("WORD");
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());
		this.add(getSelectedCheckBox(), gridBagConstraints);
		this.add(textLabel, gridBagConstraints1);
		this.add(countLabel, gridBagConstraints2);
	}

	/**
	 * This method initializes selectedCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getSelectedCheckBox() {
		if (selectedCheckBox == null) {
			selectedCheckBox = new JCheckBox();
			selectedCheckBox.addChangeListener(this);
		}
		return selectedCheckBox;
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if(e.getSource() == selectedCheckBox) {
			this.setBackground((selectedCheckBox.isSelected() ? SELECTEDCOLOR : DEFAULTCOLOR));
		}
	}
	
	protected Word getWord() {
		return word;
	}

}
